#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;


class Solution {
public:
    bool sta[400010];
    string res;

    bool judge(int x)
    {
        for (int i = 0; i < x; i++)
        {
            if (sta[i] == true) return false;
        }
        return true;
    }

    void dfs(int x, int n, string& s)
    {
        if (x == n)
        {
            string s1;
            s1.reserve(n);
            for (int i = 0; i < n; i++)
            {
                if (sta[i])
                    s1.push_back(s[i]);
            }
            if (res.size() == 0 || (strcmp(res.c_str(), s1.c_str()) < 0))
            {
                res = s1;
            }
            return;
        }
        if (x == 0 || sta[x - 1] == true || judge(x))
        {
            sta[x] = true;
            dfs(x + 1, n, s);
        }
        sta[x] = false;
        dfs(x + 1, n, s);
    }

    string lastSubstring(string s) {
        dfs(0, s.size(), s);
        return res;
    }
};